using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Runtime.CompilerServices;

namespace ProductManagement.Models
{
    public class Product : INotifyPropertyChanged
    {
        private int _id;
        private string _name = string.Empty;
        private string _description = string.Empty;
        private decimal _price;
        private int _stock;
        private string _category = string.Empty;
        private DateTime _createDate = DateTime.Now;
        private bool _isActive = true;

        public int Id
        {
            get { return _id; }
            set { SetProperty(ref _id, value); }
        }

        [Required(ErrorMessage = "产品名称不能为空")]
        [StringLength(100, ErrorMessage = "产品名称长度不能超过100个字符")]
        public string Name
        {
            get { return _name; }
            set { SetProperty(ref _name, value); }
        }

        [StringLength(500, ErrorMessage = "描述长度不能超过500个字符")]
        public string Description
        {
            get { return _description; }
            set { SetProperty(ref _description, value); }
        }

        [Range(0.01, double.MaxValue, ErrorMessage = "价格必须大于0")]
        public decimal Price
        {
            get { return _price; }
            set { SetProperty(ref _price, value); }
        }

        [Range(0, int.MaxValue, ErrorMessage = "库存不能为负数")]
        public int Stock
        {
            get { return _stock; }
            set { SetProperty(ref _stock, value); }
        }

        public string Category
        {
            get { return _category; }
            set { SetProperty(ref _category, value); }
        }

        public DateTime CreateDate
        {
            get { return _createDate; }
            set { SetProperty(ref _createDate, value); }
        }

        public bool IsActive
        {
            get { return _isActive; }
            set { SetProperty(ref _isActive, value); }
        }

        public event PropertyChangedEventHandler? PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        protected bool SetProperty<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
        {
            if (EqualityComparer<T>.Default.Equals(field, value)) return false;
            field = value;
            OnPropertyChanged(propertyName);
            return true;
        }
    }
}